package com.lzy.business.controller;

import com.lzy.base.apis.SysResourceServiceApi;
import com.lzy.base.constants.Constants;
import com.lzy.base.dtos.SysResourceDto;
import com.lzy.base.utils.UserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lc
 * @date 2017年1月1日 下午6:03:38
 */
@Controller
public class IndexController {
    private static final Logger LOGGER = LoggerFactory
            .getLogger(IndexController.class);
    @Resource
    private SysResourceServiceApi sysResourceService;

    /**
     * 登录后进入首页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "admin/index", method = RequestMethod.GET)
    public String initIndex(Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated() || subject.isRemembered()) {
            List<SysResourceDto> menuRootList = sysResourceService.selectResource(
                    UserUtil.getCurrentUser().getId(),
                    Constants.RESOURCE_ADMIN_MENU, 0L,
                    Constants.RESOURCE_STATUS_ABLE, null);
            model.addAttribute("menuRoot", menuRootList);
            model.addAttribute("loginName", UserUtil.getCurrentUser().getLoginName());
            return "admin/index";
        }
        return "admin/login";
    }

    /**
     * 首页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"/index", "/", "", "/admin", "/login"}, method = RequestMethod.GET)
    public String index(Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated() || subject.isRemembered()) {
            model.addAttribute("loginName", UserUtil.getCurrentUser().getLoginName());
            return "redirect:/admin/index";
        }
        return "admin/login";
    }

    /**
     * 头部菜单栏获取
     *
     * @return
     */
    @RequestMapping("admin/headerMenu")
    @ResponseBody
    public List<SysResourceDto> headerMenuAjax() {
        List<SysResourceDto> sysMenus = sysResourceService.selectResource(
                UserUtil.getCurrentUser().getId(),
                Constants.RESOURCE_ADMIN_MENU, 0L,
                Constants.RESOURCE_STATUS_ABLE, null);
        return sysMenus;
    }

    /**
     * 左侧菜单栏获取
     *
     * @param id
     * @return
     */
    @RequestMapping("admin/menu")
    @ResponseBody
    public List<SysResourceDto> menuAjax(@RequestParam("id") Long id) {
        List<SysResourceDto> sysMenus = sysResourceService.selectAllResource(
                UserUtil.getCurrentUser().getId(),
                Constants.RESOURCE_ADMIN_MENU, id,
                Constants.RESOURCE_STATUS_ABLE, null);
        return sysMenus;
    }

    /**
     * 登录后进入首页内容页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "admin/home", method = RequestMethod.GET)
    public String initIndexContent(Model model) {
        model.addAttribute("loginName", UserUtil.getCurrentUser().getLoginName());
        return "admin/index";
    }

}
